package com.hnzbxy.api.biz.controller;

import com.hnzbxy.api.biz.entity.Teacher;
import com.hnzbxy.api.biz.service.TeacherService;
import com.hnzbxy.api.biz.vo.TeacherVO;
import com.hnzbxy.api.common.annotation.ControllerEndpoint;
import com.hnzbxy.api.common.bean.ResponseBean;
import com.hnzbxy.api.system.vo.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教员管理接口（对齐UserController风格：分页+条件查询）
 * @Author xiaoge
 * @Date 2020/3/10 11:51
 * @Version 1.0
 **/
@Api(tags = "教员管理接口")
@RequestMapping("/teacher")
@RestController
@CrossOrigin
@Validated
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    /**
     * 教员列表（分页+条件查询）
     * 匹配前端Vue查询字段：姓名、性别、手机号、邮箱
     */
    @ApiOperation(value = "教员列表", notes = "分页查询教员列表，支持姓名/性别/手机号/邮箱条件筛选")
    @RequiresPermissions({"teacher:list"})
    @GetMapping("/list")
    public ResponseBean list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                             TeacherVO teacherVO) {
        PageVO<TeacherVO> pageVO = teacherService.findTeacherList(pageNum, pageSize, teacherVO);
        return ResponseBean.success(pageVO);
    }

    /**
     * 新增教员
     */
    @ControllerEndpoint(exceptionMessage = "新增教员失败", operation = "新增教员")
    @ApiOperation(value = "新增教员", notes = "传入教员基本信息（姓名、性别必传）")
    @RequiresPermissions({"teacher:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody @Validated TeacherVO teacherVO) {
        Teacher teacher = teacherService.add(teacherVO);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("id", teacher.getId());
        resultMap.put("name", teacher.getName());
        return ResponseBean.success(resultMap);
    }

    /**
     * 删除教员
     */
    @ControllerEndpoint(exceptionMessage = "删除教员失败", operation = "删除教员")
    @ApiOperation(value = "删除教员", notes = "根据ID删除指定教员")
    @RequiresPermissions({"teacher:delete"})
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        teacherService.delete(id);
        return ResponseBean.success();
    }

    /**
     * 教员详情（确保返回VO包含id）
     */
    @ApiOperation(value = "教员详情", notes = "根据ID获取教员信息，用于编辑页回显（含id）")
    @RequiresPermissions({"teacher:edit"})
    @GetMapping("/edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        TeacherVO teacherVO = teacherService.edit(id);
        return ResponseBean.success(teacherVO);
    }

    /**
     * 更新教员信息
     */
    @ControllerEndpoint(exceptionMessage = "更新教员失败", operation = "更新教员")
    @ApiOperation(value = "更新教员", notes = "根据ID更新教员信息（姓名、性别必传）")
    @RequiresPermissions({"teacher:update"})
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,
                               @RequestBody @Validated TeacherVO teacherVO) {
        teacherService.update(id, teacherVO);
        return ResponseBean.success();
    }

    /**
     * 导出教员Excel（支持条件导出）
     */
    @ApiOperation(value = "导出教员Excel", notes = "导出当前筛选条件下的教员信息到Excel表格")
    @RequiresPermissions({"teacher:export"})
    @PostMapping("/exportExcel")
    @ControllerEndpoint(exceptionMessage = "导出教员Excel失败", operation = "导出教员Excel")
    public void exportExcel(HttpServletResponse response, TeacherVO teacherVO) {
        List<Teacher> list = teacherService.findAllByCondition(teacherVO);
        ExcelKit.$Export(Teacher.class, response).downXlsx(list, false);
    }

}